package com.ivo.biz;


import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.ivo.core.WebConstants;
import com.ivo.quartz.AryPerformanceQuartz;
import com.ivo.sec.SessionLocal;
import com.ivo.sys.user.User;
import com.ivo.util.JsonUtil;
import com.ivo.util.WebUtils;


@RestController
@RequestMapping("/sys/performance")
public class PublicController {
	
	@Autowired
	private PerformanceService performanceService;
	
	
	
	String view = "";
	
	@RequestMapping(value = "/Permissions")
	@ResponseBody
	public ModelAndView Permissions(HttpServletRequest request){
		if(SessionLocal.getUser().getFactory().equals("ARY")){
			view = "sys/ary";
		}else if(SessionLocal.getUser().getFactory().contains("M")){
			view = "sys/lcm";
		}
		ModelAndView mv = new ModelAndView(view);
		mv.addObject("category","Permissions");
		return mv;
	}
	
	
	@RequestMapping(value = "/User")
	@ResponseBody
	public ModelAndView User(HttpServletRequest request){
		if(SessionLocal.getUser().getFactory().equals("ARY")){
			view = "sys/ary";
		}else if(SessionLocal.getUser().getFactory().contains("M")){
			view = "sys/lcm";
		}
		ModelAndView mv = new ModelAndView(view);
		mv.addObject("category","User");
		return mv;
	}
	
	@RequestMapping(value = "/Password")
	@ResponseBody
	public ModelAndView Password(HttpServletRequest request){
		if(SessionLocal.getUser().getFactory().equals("ARY")){
			view = "sys/ary";
		}else if(SessionLocal.getUser().getFactory().contains("M")){
			view = "sys/lcm";
		}
		ModelAndView mv = new ModelAndView(view);
		mv.addObject("category","Password");
		return mv;
	}
	
	
	@RequestMapping(value = "/addUser")
    public String addUser(HttpServletRequest request,
			 @RequestParam(value = "emp_id", required = false) String emp_id,
			 @RequestParam(value = "scope", required = false) String scope,
	    	 @RequestParam(value = "emp_name", required = false) String emp_name){
    	String err_code = "";
    	String err_msg = "";
    	int r = performanceService.addUser(emp_id,emp_name,scope);
    	if(r==1){
    		err_code = "0";
    		err_msg = "success";
    	}else if(r==0){
    		err_code = "1";
    		err_msg = "failed";
    	}else{
    		err_code = "2";
    		err_msg = "exist";
    	}
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }

		@RequestMapping(value = "/getUsers")
		@ResponseBody
		public String getUsers(
				HttpServletRequest request,
				@RequestParam(value = "allFlag", required = false) int allFlag,
				@RequestParam(value = "pno", required = false) int pno,
				@RequestParam(value = "emp_id", required = false) String emp_id
				) throws ParseException{
			Map<String, Object> returnMap = new HashMap<String, Object>();
			returnMap.put("err_code", "0");
		    returnMap.put("err_msg", "success");
		    returnMap.put("result", performanceService.getUsers(emp_id,pno,allFlag));
		    return JsonUtil.toJson(returnMap);
		}

	
	
	 @RequestMapping(value = "/addPermissions")
	    public String addPermissions(HttpServletRequest request,
				 @RequestParam(value = "emp_id", required = false) String emp_id,
		    	 @RequestParam(value = "scope", required = false) String scope,
		    	 @RequestParam(value = "emp_name", required = false) String emp_name,
		    	 @RequestParam(value = "ope_id[]", required = false) String[] ope_id) {
	    	String err_code = "";
	    	String err_msg = "";
	    	int r = performanceService.addPermissions(emp_id,scope,ope_id,emp_name);
	    	if(r==1){
	    		err_code = "0";
	    		err_msg = "success";
	    	}else{
	    		err_code = "1";
	    		err_msg = "failed";
	    	}
	    	Map<String, Object> returnMap = new HashMap<String, Object>();
	    	returnMap.put("err_code", err_code);
	        returnMap.put("err_msg", err_msg);
	        return JsonUtil.toJson(returnMap);
	    }
	
	  
	  
	  
	  
	  
	
	
	@RequestMapping(value = "/getPermissions")
	@ResponseBody
	public String getPermissions(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getPermissions(emp_id,pno,allFlag));
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	
	@RequestMapping(value = "/deletePermissions")
    public String delete(
    		HttpServletRequest request,
    		@RequestParam(value = "emp_id", required = false) String emp_id
    		) {
    	String err_code = "";
    	String err_msg = "";
    	int r = performanceService.deletePermissions(emp_id);
    	if(r==1){
    		err_code = "0";
    		err_msg = "success";
    	}
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
	
    
    
	@RequestMapping(value = "/isUserExist")
    public String isUserExist(
    		HttpServletRequest request,
    		@RequestParam(value = "emp_id", required = false) String emp_id
    		) {
    	String err_code = "";
    	String err_msg = "";
    	String user_id = performanceService.isUserExist(emp_id);
    	if(user_id != null){
    		err_code = "1";
    		err_msg = user_id;
    	}
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
    
    
    
	@RequestMapping(value = "/deleteUser")
    public String deleteUser(
    		HttpServletRequest request,
    		@RequestParam(value = "emp_id", required = false) String emp_id
    		) {
    	String err_code = "";
    	String err_msg = "";
    	int r = performanceService.deleteUser(emp_id);
    	if(r==1){
    		err_code = "0";
    		err_msg = "success";
    	}
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
    
    
    
    
	@RequestMapping(value = "/changePassword")
    public String changePassword(
    		HttpServletRequest request,
    		@RequestParam(value = "oldpassword", required = false) String oldpassword,
    		@RequestParam(value = "newpassword", required = false) String newpassword
    		) {
    	String err_code = "";
    	String err_msg = "";
    	int r = performanceService.changePassword(oldpassword,newpassword);
    	if(r==1){
    		err_code = "0";
    		err_msg = "success";
    	}
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
    
    
    
    
    @RequestMapping(value = "/getUser_ID")
	@ResponseBody
	public String getUser_ID(
    		HttpServletRequest request,
    		@RequestParam(value = "emp_id", required = false) String emp_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getUser_ID(emp_id));
        return JsonUtil.toJson(returnMap);
    }
    
	

	
	
	
	
    @RequestMapping(value = "/getadmin", method = RequestMethod.POST)
    @ResponseBody
    public String getadmin(HttpServletRequest request) {
        String err_code = "";
    	String err_msg = "";
    	int r = performanceService.getadmin();
    	if(r==0){
    		err_code = "0";
    		err_msg = "false";
    	}else{
    		err_code = "1";
    		err_msg = "true";
    	}
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
	
	
	

		
	
}
